#include <iostream>

#include "ftp_users_configuration_reader.h"
#include "access_control_matrix.h"

namespace AUTH = gatech::cs6238::gtfilemonitor::authz;

int main(int argc, char **argv)
{	
	//AUTH::FtpUser user("TestUser","1");
	//AUTH::FtpGroup group("TestGroup","2");
	
	//group.RegisterUserInGroup(user);
	
	//std::cout << "Does group:"<< group.GetGroupName() << " have user:" << user.GetUserName() << ": " << group.IsUserMember(user) << std::endl;
	//std::cout << "Does User:"<< user.GetUserName() << " have group:" << group.GetGroupName() << ": " << user.IsUserMemberOfGroup(group) << std::endl;
	
	//AUTH::FtpUsersConfigurationReader* reader = AUTH::FtpUsersConfigurationReader::Instance();
	
	/*
	AUTH::FtpUsersConfigurationReader* reader = new AUTH::FtpUsersConfigurationReader();
	if(reader->IsInitialized() == false)
	{
		reader->Initialize();
	}
	
	AUTH::FtpUsersConfigurationReader::UserListType::iterator usrIter = reader->m_UserList.begin();
	for(; usrIter != reader->m_UserList.end(); ++usrIter)
	{
		std::cout << "User Id: " << usrIter->first << " Name: " << usrIter->second->GetUserName() << std::endl;
	}
	
	AUTH::FtpUser* usr = reader->m_UserList[(uid_t)1000];

	AUTH::FtpUsersConfigurationReader::GroupListType::iterator grpIter = reader->m_GroupList.begin();
	for(; grpIter != reader->m_GroupList.end(); ++grpIter)
	{
		std::cout << "Group Id: " << grpIter->first << " Name: " << grpIter->second->GetGroupName() << std::endl;
		if(usr->IsUserMemberOfGroup(grpIter->second))
		{
			std::cout << "\t\t" << usr->GetUserName() << " is a member" << std::endl;
		}
	}		
	
	delete reader;
	reader = NULL;
	//AUTH::FtpUsersConfigurationReader::Release();
	 */ 
	
	AUTH::AccessControlMatrix* instance = AUTH::AccessControlMatrix::Instance();
	
	instance->Initialize("/home/keerthi/acl_config.txt");
	
	AUTH::FtpUser* user = instance->GetUserConfiguration()->GetUserById(1000);
	
	bool shouldHaveAccess = instance->ShouldAllowAccess("/home/keerthi/test/test.txt", AUTH::READ_ACCESS, *user);
	std::cout << "User Should have read access. Calculated is: " << std::boolalpha << shouldHaveAccess << std::endl;
	
	shouldHaveAccess = instance->ShouldAllowAccess("/home/keerthi/test/test.txt", AUTH::EXECUTE_ACCESS, *user);
	std::cout << "User Should not have execute access. Calculated is: " << std::boolalpha << shouldHaveAccess << std::endl;

	shouldHaveAccess = instance->ShouldAllowAccess("/home/keerthi/test/test1.txt", AUTH::READ_ACCESS, *user);
	std::cout << "User Should not have read access. Calculated is: " << std::boolalpha << shouldHaveAccess << std::endl;

	user = instance->GetUserConfiguration()->GetUserById(1001);
	
	shouldHaveAccess = instance->ShouldAllowAccess("/home/keerthi/test/test2.txt", AUTH::WRITE_ACCESS, *user);
	std::cout << "User Should not have write access. Calculated is: " << std::boolalpha << shouldHaveAccess << std::endl;

	AUTH::AccessControlMatrix::Release();
	 
	return 0;
}
